package vista;
import com.cloudgarden.layout.AnchorLayout;

import contoladores.CProducto;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.postgresql.util.GT;


public class VProducto extends javax.swing.JFrame {
	private JLabel jLblcod_categoria;
	private JLabel jLblcodigo;
	private JButton jButtonCancelar;
	private JLabel jLabel1;
	private JTextField jTxtcod;
	private JButton Salir;
	private JButton jButtonAgregar;
	private JTextField jTxtdescrip;
	private JComboBox jComboBoxCategoria;
	private JLabel jLabel2;
	private JTextField jTextFieldPrecio;
	private JLabel jLabel4;
	private JComboBox jComboBoxIngrediente;
	private JSpinner jSpinnerPorcion;
	private JLabel jLabel8;
	private JButton jButtonMas;
	private JButton jButtonMenos;
	private JTable jTable1;
	private JLabel jLbllIngredientes;
	private JLabel jLbldescripcion;
	private JLabel jLblDescripcion;
	private DefaultTableModel modelo;
	private CProducto control;

	
	public VProducto(CProducto producto)  {
		super();
		initGUI();
		control=producto;
	}
	private void initGUI() {
		{
			getContentPane().setLayout(null);
			this.setTitle("Producto");
		}
		{
			jLbldescripcion = new JLabel();
			getContentPane().add(jLbldescripcion, "North");
			FlowLayout jLbldescripcionLayout = new FlowLayout();
			jLbldescripcion.setText("Nombre:");
			jLbldescripcion.setLayout(null);
			jLbldescripcion.setBounds(31, 42, 95, 15);
		}
		{
			jLblcodigo = new JLabel();
			getContentPane().add(jLblcodigo, "Center");
			FlowLayout jLblcodigoLayout = new FlowLayout();
			jLblcodigo.setText("Codigo:");
			jLblcodigo.setLayout(null);
			jLblcodigo.setBounds(31, 7, 180, 22);
		}
		{
			jTxtcod = new JTextField();
			getContentPane().add(jTxtcod, "West");
			jTxtcod.setBounds(122, 8, 102, 22);
		}
		{
			jTxtdescrip = new JTextField();
			getContentPane().add(jTxtdescrip, "West");
			jTxtdescrip.setBounds(121, 42, 238, 22);
		}
		{
			jButtonAgregar = new JButton();
			getContentPane().add(jButtonAgregar);
			jButtonAgregar.setText("Agregar");
			jButtonAgregar.setBounds(102, 356, 101, 22);
			jButtonAgregar.setActionCommand("Agregar");
		}
		{
			Salir = new JButton();
			getContentPane().add(Salir);
			Salir.setBounds(300, 411, 94, 22);
			Salir.setText("Salir");
			Salir.setActionCommand("Salir");
		}
		{
			jLabel1 = new JLabel();
			getContentPane().add(jLabel1);
			jLabel1.setText("Precio:");
			jLabel1.setBounds(31, 73, 71, 15);
		}
		{
			jButtonCancelar = new JButton();
			getContentPane().add(jButtonCancelar);
			jButtonCancelar.setText("Limpiar");
			jButtonCancelar.setBounds(227, 356, 104, 22);
			jButtonCancelar.setActionCommand("Cancelar");
		}
		{
			jLbllIngredientes = new JLabel();
			getContentPane().add(jLbllIngredientes);
			jLbllIngredientes.setText("Ingredientes para la Elaboracion del Producto:");
			jLbllIngredientes.setBounds(26, 261, 327, 15);
		}
		{
			TableModel jTable1Model = 
				new DefaultTableModel(
						new String[][] { { "One", "Two" }, { "Three", "Four" } },
						new String[] { "Column 1", "Column 2" });
			jTable1 = new JTable();
			getContentPane().add(jTable1);
			jTable1.setModel(jTable1Model);
			jTable1.setBounds(78, 288, 239, 56);
			jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		}
		{
			jButtonMenos = new JButton();
			getContentPane().add(jButtonMenos);
			jButtonMenos.setText("-");
			jButtonMenos.setBounds(102, 202, 54, 22);
			jButtonMenos.setActionCommand("Menos");
		}
		{
			jButtonMas = new JButton();
			getContentPane().add(jButtonMas);
			jButtonMas.setText("+");
			jButtonMas.setBounds(31, 202, 58, 22);
			jButtonMas.setActionCommand("Mas");
		}
		{
			jLabel8 = new JLabel();
			getContentPane().add(jLabel8);
			jLabel8.setText("Porcion:");
			jLabel8.setBounds(31, 177, 65, 15);
		}
		{
			Vector<String> v = new Vector<String>();
			String patron = "0.0";
			DecimalFormat nf = new DecimalFormat(patron); 
			nf.applyPattern(patron);
			
			double i =0.1;
			while(i<=100){
				v.add(nf.format(i));
				i+=0.1;
				
			}
			SpinnerListModel jSpinnerPorcionModel = 
				new SpinnerListModel(
						new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" });
			jSpinnerPorcion = new JSpinner();
			jSpinnerPorcionModel.setList(v);
			getContentPane().add(jSpinnerPorcion);
			jSpinnerPorcion.setModel(jSpinnerPorcionModel);
			jSpinnerPorcion.setBounds(114, 174, 76, 22);
		}
		{
			ComboBoxModel jComboBoxProductoModel = 
				new DefaultComboBoxModel(
						new String[] { "Item One", "Item Two" });
			jComboBoxIngrediente = new JComboBox();
			getContentPane().add(jComboBoxIngrediente);
			jComboBoxIngrediente.setModel(jComboBoxProductoModel);
			jComboBoxIngrediente.setBounds(120, 132, 133, 22);
		}
		{
			jLabel4 = new JLabel();
			getContentPane().add(jLabel4);
			jLabel4.setText("Ingrediente:");
			jLabel4.setBounds(31, 136, 92, 15);
		}
		{
			jTextFieldPrecio = new JTextField();
			getContentPane().add(jTextFieldPrecio);
			jTextFieldPrecio.setBounds(120, 70, 239, 22);
		}
		{
			jLabel2 = new JLabel();
			getContentPane().add(jLabel2);
			jLabel2.setText("Categoria");
			jLabel2.setBounds(31, 102, 92, 15);
		}
		{
			ComboBoxModel jComboBox1Model = 
				new DefaultComboBoxModel(
						new String[] { "Item One", "Item Two" });
			jComboBoxCategoria = new JComboBox();
			getContentPane().add(jComboBoxCategoria);
			jComboBoxCategoria.setModel(jComboBox1Model);
			jComboBoxCategoria.setSelectedIndex(0);
			jComboBoxCategoria.setBounds(122, 98, 133, 22);
		}
		{
			this.setSize(429, 474);
		}
		modelo = new DefaultTableModel();
		jTable1.setModel(modelo);
		Vector<String> v = new Vector<String>();
		v.add("Ingrediente");
		v.add("Cantidad Usada");
		modelo.setColumnIdentifiers(v);
		
	}
	
	public void addListener(ActionListener escuchador){
		jButtonAgregar.addActionListener(escuchador);
		jButtonMas.addActionListener(escuchador);
		jButtonMenos.addActionListener(escuchador);
		Salir.addActionListener(escuchador);
		jButtonCancelar.addActionListener(escuchador);
	}
	
	public DefaultTableModel getModelo() {
		return modelo;
	}


	public float getPorcion(){
		String nf = ""+jSpinnerPorcion.getValue();		
		return Float.parseFloat(nf.substring(0,nf.indexOf(","))) + Float.parseFloat(nf.substring(nf.indexOf(",")+1,nf.length()));
		
	}
	
	public  void  cargarCategorias(Vector<String> v){
		jComboBoxCategoria.removeAllItems();
		for (Iterator iterator = v.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			jComboBoxCategoria.addItem(string);
	}}
	
	public  void  cargarIngredientes(Vector<String> v){
		jComboBoxIngrediente.removeAllItems();
		for (Iterator iterator = v.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			jComboBoxIngrediente.addItem(string);
	}}
	
	public void limpiar(){
		jTxtcod.setText("");
		jTxtdescrip.setText("");
		jTextFieldPrecio.setText("");
		jSpinnerPorcion.setValue("0,1");
		jComboBoxIngrediente.setSelectedIndex(0);
		jComboBoxCategoria.setSelectedIndex(0);
		modelo.setRowCount(0);
	}
	
	public  String getCat(){
		return  (String) jComboBoxCategoria.getItemAt(jComboBoxCategoria.getSelectedIndex());
	}
	
	public  String getIng(){
		return  (String) jComboBoxIngrediente.getItemAt(jComboBoxIngrediente.getSelectedIndex());
	}

	public  String getCod(){
		return  jTxtcod.getText();
	}
	
	public String getPre(){
		return jTextFieldPrecio.getText();
	}
	
	public String getDes(){
		return jTxtdescrip.getText();
	}
	
	public void agregar(){
		String d = getIng();  
		String c = ""+getPorcion();
		System.out.println(d+" "+c);
		int i =0;
		System.out.println(modelo.getRowCount());
		while(i<modelo.getRowCount()){
			if(d.equals((String) modelo.getValueAt(i, 0))){
				modelo.setValueAt(c, i, 1);
				return;
			}
			i++;
		}
		Vector<String> v = new Vector<String>();
		v.add(d);
		v.add(c);
		modelo.addRow(v);
	}
	
	public void quitar(){
		int i =jTable1.getSelectedRow();
		if(i!=-1)
			modelo.removeRow(i);
	}


}
